package com.gsma.calendar.android.ui;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;

import com.gsma.calendar.android.R;
import com.gsma.calendar.android.adapter.GlobalEventAdapter;
import com.gsma.calendar.android.db.CalendarColumns;
import com.gsma.calendar.android.db.CalendarGsmaQuery;
import com.gsma.calendar.android.db.GlobalEventQuery;
import com.gsma.calendar.android.model.CalendarObject;
import com.gsma.calendar.android.model.GlobalEvent;
import com.gsma.calendar.android.util.CalendarGsmaUtil;
import com.gsma.calendar.android.util.Config;

public class GlobalEventsActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks<Cursor>, OnItemClickListener {
	private Spinner spinner;
	private ListView listView;
	private GlobalEventAdapter adapter;
	private Cursor cursorCalendar;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.global_event);

		spinner = (Spinner) findViewById(R.id.spinner);
		listView = (ListView) findViewById(R.id.list_global_events);
		listView.setOnItemClickListener(this);

		getSupportLoaderManager().initLoader(GlobalEventQuery._TOKEN, null, this);

		String[] projection = new String[] { "_id", "name" };
		String selection = "selected=1";
		String path = "calendars";

		CalendarGsmaUtil calen = new CalendarGsmaUtil(GlobalEventsActivity.this);
		cursorCalendar = calen.getCalendarManagedCursor(projection, selection, path);

		SimpleCursorAdapter countriesAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, cursorCalendar, new String[] { "name" },
				new int[] { android.R.id.text1 });

		spinner.setAdapter(countriesAdapter);
		countriesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	}

	@Override
	public Loader<Cursor> onCreateLoader(int id, Bundle bundle) {
		switch (id) {
		case GlobalEventQuery._TOKEN:
			return new CursorLoader(GlobalEventsActivity.this, GlobalEvent.CONTENT_URI, GlobalEventQuery.PROJECTION, null, null, GlobalEvent.START_TIME_SORT);
		default:
			return null;
		}
	}

	@Override
	public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
		switch (loader.getId()) {
		case GlobalEventQuery._TOKEN:
			if (cursor != null && cursor.moveToFirst()) {
				startManagingCursor(cursor);
				adapter = new GlobalEventAdapter(GlobalEventsActivity.this, cursor);
				listView.setAdapter(adapter);
			}
			break;
		default:
			break;
		}
	}

	@Override
	public void onLoaderReset(Loader<Cursor> arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
		Intent intent = new Intent(GlobalEventsActivity.this, GlobalEventDetail.class);
		String nameEvent = null;
		GlobalEventAdapter adapter = (GlobalEventAdapter) adapterView.getAdapter();
		Cursor cursor = adapter.getCursor();
		if (cursor != null && cursor.moveToPosition(position)) {
			nameEvent = cursor.getString(GlobalEventQuery.EVENT);
		}
		intent.putExtra(Config.EVENT, nameEvent);
		startActivity(intent);
	}

	public void addChecked(View view) {

		long calendarId = spinner.getSelectedItemId();
		if (adapter != null) {
			ArrayList<String> list = adapter.getListChecked();
			for (int i = 0; i < list.size(); i++) {
				Log.e("", list.get(i));
				// Temporary in UI
				Cursor cursor = managedQuery(CalendarObject.CONTENT_URI, CalendarGsmaQuery.PROJECTION, CalendarColumns.EVENT.getName() + "=?",
						new String[] { list.get(i) }, null);

				if (cursor != null && cursor.moveToFirst()) {

					// Intent intent = new Intent(Intent.ACTION_EDIT);
					// intent.setType("vnd.android.cursor.item/event");
					// intent.putExtra("calendar_id", calendarId);
					// intent.putExtra("title",
					// cursor.getString(CalendarGsmaQuery.SESSION));
					// intent.putExtra("description",
					// cursor.getString(CalendarGsmaQuery.WHERE));
					// intent.putExtra("beginTime",
					// cursor.getLong(CalendarGsmaQuery.START_TIME));
					// intent.putExtra("endTime",
					// cursor.getLong(CalendarGsmaQuery.END_TIME));
					// startActivity(intent);

					CalendarGsmaUtil calendar = new CalendarGsmaUtil(GlobalEventsActivity.this);
//					calendar.makeNewCalendarEntry((int) calendarId, cursor);
					cursor.close();
					
					
					calendar.listAllCalendarEntries((int) calendarId);
				}

			}
		}
	}

}